<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 订单接口
 */
class Order extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //获取订单列表
    public function getorderlist(){
        $user = $this->auth->getUser();
        $status = input('status','');
        if($status){
            $where['status'] = $status;
        }
        $where['user_id'] = $user->id;
        $where['isdel'] = 0;
        $page = input('page',1);
        $limit = input('limit',10);
        $list = db('userorder')->where($where)->order('createtime desc')->page($page,$limit)->select();
        foreach($list as $k=>$v){
            if($v['type']==1){
                $info = db('weekorder')->where(['order_sn'=>$v['order_sn']])->find();//周期购订单
                $info['createtime'] = date('Y-m-d H:i',$info['createtime']);
                $weekgoods = db('weekgoods')->where(['id'=>$info['weekgoods_id']])->select();
                $list[$k]['info'] = $info;
                $list[$k]['weekgoods'] = $weekgoods;
            }else{
                $info = db('orders')->where(['order_num'=>$v['order_sn']])->find();//普通订单
                $info['createtime'] = date('Y-m-d H:i',$info['createtime']);
                $ordersgood = db('ordersgood')->where(['order_id'=>$info['id']])->select();
                $list[$k]['info'] = $info;
                $list[$k]['ordersgood'] = $ordersgood;
            }
        }
        $this->success('ok',$list);
    }
    //取消订单
    public function cancelorder(){
        $order_id = input('order_id','');
        $order = db('userorder')->where(['id'=>$order_id])->find();
        if($order['status']!=1){
            $this->error('该订单不可取消');
        }
        db('userorder')->where(['id'=>$order_id])->update(['status'=>5,'canceltime'=>time()]);
        if($order['type']==1){
            db('weekorder')->where(['order_sn'=>$order['order_sn']])->update(['status'=>4]);
        }else{
            $orders = db('orders')->where(['order_num'=>$order['order_sn']])->find();
            db('orders')->where(['order_num'=>$order['order_sn']])->update(['status'=>5]);
            //增加库存
            $order_id = $orders['id'];
            $ordergoods = db('ordersgood')
                ->where('order_id',$order_id)
                ->select();
            foreach ($ordergoods as $ordergood){
                db('productssku')
                    ->where('id',$ordergood['productssku_id'])
                    ->setInc('stock',$ordergood['num']);
                db('products')
                    ->where('id',$ordergood['products_id'])
                    ->setDec('sellnum',$ordergood['num']);
            }
        }
        $this->success('取消成功');
    }
    //提醒发货
    public function remindorder()
    {
        $user = $this->auth->getUser();
        $order_id = input('order_id');
        $order = db('userorder')->where(['id'=>$order_id])->find();
        $order = db('orders')
            ->where([
                'order_num'=>$order['order_sn'],
                'user_id'=>$user->id
            ])
            ->find();
        if (!$order){
            $this->error('订单不存在');
        }
        if ($order['status'] != 2){
            $this->error('订单状态错误');
        }
        //更新订单状态 
        db('orders')
            ->where('id',$order['id'])
            ->update([
                'is_tixing'=>2,
                'updatetime'=>time()
            ]);
        $this->success('提醒成功');
    }
    public function confirmorder()
    {
        $user = $this->auth->getUser();
        $order_id = input('order_id');
        $order = db('userorder')->where(['id'=>$order_id])->find();
        $order = db('orders')
            ->where([
                'order_num'=>$order['order_sn'],
                'user_id'=>$user->id
            ])
            ->find();
        if (!$order){
            $this->error('订单不存在');
        }
        if ($order['status'] != 3){
            $this->error('订单状态错误');
        }
        //更新订单状态
        db('orders')
            ->where('id',$order['id'])
            ->update([
                'status'=>4,
                'entruetime'=>time()
            ]);
        db('userorder')->where(['order_sn'=>$order['order_sn']])->update(['status'=>4]);
        $this->success('确认成功');
    }
    //删除订单
    public function delorder()
    {
        $user = $this->auth->getUser();
        $order_id = input('order_id');
        $order = db('userorder')->where(['id'=>$order_id])->find();
        // $order_id = input('order_id');
        if($order['type']==1){
            db('weekorder')->where(['order_sn'=>$order['order_sn']])->update(['isdel'=>1]);
        }else{
            db('orders')
            ->where('order_num',$order['order_sn'])
            ->update([
                'deletetime'=>time()
            ]);
        }
        db('userorder')->where(['id'=>$order_id])->update(['isdel'=>1]);
        $this->success('ok');
    }

}
